<template>
  <div>
    <!--面包屑导航区-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--卡片-->
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" >添加角色</el-button>
        </el-col>
      </el-row>
      <!--表-->
      <el-table :data="rolesList" border stripe>
        <!--展开列-->
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row
            v-for="(item1,index1) in scope.row.children"
            :key="index1"
            :class="['rowBut',index1==0?'rowTop':'','center']">
              <el-col :span="1"></el-col>
              <!--一级权限-->
              <el-col :span="4" class="rowCenter">
                <el-tag :key="item1.id" closable size="mini" @close="delRoleById(scope.row,item1.id)">{{item1.name}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!--二级权限和三级权限-->
              <el-col :span="19">
                <!--循环二级权限-->
                <el-row
                v-for="(item2,index2) in item1.children"
                :key="index1+'_'+index2"
                :class="['rowBut',index2==0?'rowTop':'','center']">
                  <el-col :span="6">
                    <el-tag type="success" :key="item2.id" closable size="mini" @close="delRoleById(scope.row,item2.id)">{{item2.name}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                     <!--三级权限-->
                     <el-tag type="warning" v-for="item3 in item2.children" :key="item3.id" closable size="mini" @close="delRoleById(scope.row,item3.id)">{{item3.name}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="角色名称" prop="rolename"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作" width="300px">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
            <el-button size="mini" type="warning" icon="el-icon-setting" @click="showSetRightDialog(scope.row)">分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!--分配权限 对话框-->
    <el-dialog
      title="分配权限"
      :visible.sync="setRolesdialogVisible"
      width="50%"
      @close="setRightDialogClosed">
      <el-tree
        :data="permissionTree"
        show-checkbox
        node-key="id"
        default-expand-all
        :default-checked-keys="rolesCheckedKeys"
        :props="RightsdefaultProps" ref="treeProps">
      </el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="setRolesdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="getCheckedKeys()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      queryInfo: {
        pageIndex: 1,
        pageSize: 10,
        query: ''
      },
      // 角色权限数据
      rolesList: [],
      // 控制 权限对话框的显示和隐藏
      setRolesdialogVisible: false,
      permissionTree: [],
      RightsdefaultProps: {
        children: 'children',
        label: 'name'
      },
      rolesCheckedKeys: [],
      roleId: 0
    }
  },
  methods: {
    async GetRolesList () {
      const { data: res } = await this.$http.get('/api/Roles', { params: this.queryInfo })
      const json = JSON.parse(res)
      if (json.meta.status !== 200) return this.$msg.error(this.status.msg)
      this.rolesList = json.data
      // console.log(this.rolesList)
    },
    // 删除权限
    async delRoleById (roles, pid) {
      const result = await this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (result !== 'confirm') return this.$msg.info('取消了删除')
      //  console.log(roles.id, pid)
      const { data: res } = await this.$http.delete('api/RolesPermission', { params: { rid: roles.id, pid: pid } })
      if (JSON.parse(res).meta.status !== 200) return this.$msg.error(JSON.parse(res).meta.msg)
      roles.children = JSON.parse(res).data
    },
    // 展示分配权限的对话框
    async showSetRightDialog (row) {
      this.roleId = row.id
      // 获取所有权限的数据 给树形展示
      const { data: res } = await this.$http.get('api/rights/tree')
      const json = JSON.parse(res)
      if (json.meta.status !== 200) return this.$msg.error(json.meta.msg)
      this.permissionTree = json.data
      // console.log(row)
      // 递归获取三级的ID
      this.getLeafKeys(row, this.rolesCheckedKeys)
      // 显示对话框
      this.setRolesdialogVisible = true
    },
    // 给角色加权限
    async getCheckedKeys () {
      const checkKey = this.$refs.treeProps.getCheckedKeys()
      const HalfCheckedKeys = this.$refs.treeProps.getHalfCheckedKeys()
      const arr = [...checkKey, ...HalfCheckedKeys]
      const strArr = arr.join(',')
      const res = await this.$http.put('api/roles/rights/set', { rid: this.roleId, pstr: strArr })
      console.log(res)
      this.setRolesdialogVisible = false
      this.GetRolesList()
    },
    // 通过递归的方式，获取角色的所有三级权限，并保存到 rolesCheckedKeys 中
    getLeafKeys(node, arr) {
      if (!node.children) {
        return arr.push(node.id)
      }
      node.children.forEach(item => {
        this.getLeafKeys(item, arr)
      })
    },
    // 分配权限对话框关闭事件
    setRightDialogClosed () {
      this.rolesCheckedKeys = []
    }

  },
  created () {
    this.GetRolesList()
  }
}
</script>

<style lang="less" scoped>
.el-tag{
  margin: 7px;
}
.rowTop{
  border-top: 1px solid #eee;
}
.rowBut{
  border-bottom: 1px solid #eee;
}
.rowCenter{
  align-items: center;
}
.center{
  display: flex;
  align-items: center;
}
</style>
